﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using AssetBundleHelper;
namespace HotUI{
	public class LuaView : MyLuaBehaviour,INGUIEvent {
		public string viewName;
		GameObject viewobj;
		UIPanel panel;
		MyOBJ _myobj;
		BoxCollider shade;
		int depth;
		bool yieldLoadObj;
		public object arg;
		public bool isOpen;
		public MyOBJ myobj{
			get{
				return _myobj;
			}
		}
		public GameObject getGameObject{
			get{
				return gameObject;
			}
		}
		List<LuaViewCommand> commands = new List<LuaViewCommand> ();

		public void Init(string _viewName){
			viewName = _viewName;
			shade = new GameObject ("shade").AddComponent<BoxCollider> ();
			shade.transform.parent = transform;
			shade.size = new Vector3 (10000, 10000, 0);
			GameTool.LoadUIObject ("ui_" + viewName, transform, delegate(ABObject obj) {
				viewobj = obj.gameObject;
				panel = viewobj.GetComponent<UIPanel>();
				_myobj = viewobj.GetComponent<MyOBJ>();
				GameObject.Destroy(shade);
				OnInit();
				if(yieldLoadObj){
					yieldLoadObj = false;
					OnOpen();
				}
			});
		}
		protected override void LoadLuaAssets ()
		{
			base.LoadLuaAssets ();
			DoCommand(new LVC_LuaInit("luascript",delegate(string obj) {
				if(obj != ""){
					luaScriptPath = obj;
					luaScript = GameTool.ResourcesLua (obj) as TextAsset;
					if(luaScript != null){
						Debug.Log("初始化Lua:"+luaScript.name);
						InitLua(true);
					}
				}
			}),true);
		}

		public void TryUpdateDepth(int _depth){
			DoCommand(new LVC_UpdateDepth(_depth));
		}
		public void UpdateDepth(int _depth){
			if(panel != null){
				panel.depth = _depth;
			}
		}

		public void SetDepth(int _depth){
			depth = _depth;
		}
		protected void OnInit(){
		}
		public  void OnTryOpen(object _arg){
			arg = _arg;
			isOpen = true;
			if (viewobj == null) {
				yieldLoadObj = true;
			} else {
				OnOpen ();
			}
		}
		public void DoCommand(LuaViewCommand command,bool cached = false){
			if (isOpen || cached) {	
				if (viewobj == null) {
					command.SetView (this);
					commands.Add (command);
				} else {
					command.SetView (this);
					command.Do ();
				}
			}
		}
		protected virtual void OnOpen(){
			if(panel != null)
				panel.depth = depth;
			for (var i = 0; i < commands.Count; i++) {
				commands [i].Do ();
			}
			commands.Clear ();
			CallLua("onOpen");
		}

		public virtual void OnClose(){
			isOpen = false;
			CallLua("onClose");
		}
		public void Close(){
			UIViewMgr.Instance.CloseView(viewName);
		}

		private Dictionary<string,System.Action> onClick = new Dictionary<string, System.Action>();
		private Dictionary<string,System.Action<int>> onClickItem = new Dictionary<string, System.Action<int>>();
		private Dictionary<string,System.Action<bool>> onPress = new Dictionary<string, System.Action<bool>>();
		private Dictionary<string,System.Action> onDrag = new Dictionary<string, System.Action>();

		public void LuaBindClick(string funname,System.Action callback){
			if(!onClick.ContainsKey(funname))
				onClick.Add(funname,callback);
		}
		public void LuaBindClickItem(string funname,System.Action<int> callback){
			if(!onClickItem.ContainsKey(funname))
				onClickItem.Add(funname,callback);
		}
		public void LuaBindPress(string funname,System.Action<bool> callback){
			if(!onPress.ContainsKey(funname))
				onPress.Add(funname,callback);
		}
		public void LuaBindDrag(string funname,System.Action callback){
			if(!onDrag.ContainsKey(funname))
				onDrag.Add(funname,callback);
		}

		public virtual void OnClick(NGUIAutoNotifyData data){
			if(onClick.ContainsKey(data.funName))
			   onClick[data.funName]();
		}
		public virtual void OnClickItem(NGUIAutoNotifyData data){
			if(onClickItem.ContainsKey(data.funName))
				onClickItem[data.funName](data.index);
		}
		public virtual void OnPress(NGUIAutoNotifyData data){
			if(onPress.ContainsKey(data.funName))
				onPress[data.funName](data.isPress);
		}
		public virtual void OnDrag(NGUIAutoNotifyData data){
			if(onDrag.ContainsKey(data.funName))
				onDrag[data.funName]();
		}
	}
}

